package com.example.springsecurity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springsecurity.mapper.SysUserMapper;
import com.example.springsecurity.pojo.LoginUser;
import com.example.springsecurity.pojo.SysUser;
import com.example.springsecurity.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsImpl implements UserDetailsService {
    @Autowired
//    private SysUserService sysUserService;
    private SysUserMapper sysUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("user_name", username));

        if (sysUser==null){
            throw new UsernameNotFoundException("用户不存在");
        }
        //
//        List<String> permissions = new ArrayList<>();
//        permissions.add("qwer");
        List<String> perms  = sysUserMapper.findPermsByUserId(sysUser.getId());
        LoginUser loginUser = new LoginUser(sysUser,perms);
        return loginUser;
    }
}
